package com.airlines.crewassignment.service.crew.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.airlines.common.Page;
import com.airlines.crewassignment.daofocmysql.ruleverification.CrewDutyHoursMapper;
import com.airlines.crewassignment.domain.DateTimeRange;
import com.airlines.crewassignment.domain.FlightInfo;
import com.airlines.crewassignment.domain.util.DutyDurationUtil;
import com.airlines.crewassignment.domain.util.ValueForDateUtil;
import com.airlines.crewassignment.entity.ValueForDate;
import com.airlines.crewassignment.entity.crew.CrewDutyHours;
import com.airlines.crewassignment.entity.linkline.ScheduleAuthority;
import com.airlines.crewassignment.service.crew.CrewDutyHoursService;
import com.airlines.crewassignment.service.crew.CrewFlightExperienceService;
import com.airlines.crewassignment.service.linkline.FlightInfoService;
import com.airlines.crewassignment.service.scheduleAuthority.ScheduleAuthorityService;
import com.github.pagehelper.PageInfo;

import cn.mercury.utils.JsonUtils;

/**
 * @author xxxx
 * @date 2018/8/21
 * @company xxxx Airlines
 * @description 执勤期service
 */
@Service
public class CrewDutyHoursServiceImpl implements CrewDutyHoursService {
	@Autowired(required = false)
	private CrewDutyHoursMapper crewDutyHoursMapper;
	@Autowired
	private CrewFlightExperienceService crewFlightExperienceService;
	@Autowired
	private ScheduleAuthorityService scheduleAuthorityService;
	@Autowired
	private FlightInfoService flightInfoService;

	@Override
	public String job() throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Calendar start = Calendar.getInstance();
		int year = start.get(Calendar.YEAR);
		// start.add(Calendar.DATE,-1);//昨天
		Date s = sdf.parse(year + "-01-01"); // 年初
		start.setTime(s);
		Calendar end = Calendar.getInstance();
		Date e = sdf.parse(year + "-12-31"); // 年末
		end.setTime(e);
		// 获取人员
		List<ScheduleAuthority> personList = scheduleAuthorityService.listData(new ScheduleAuthority());
		List<CrewDutyHours> allDutyHours = new ArrayList<>(); // 所有人员每天的执勤时间

		// 1.循环每天
		do {
			DateTimeRange dateTimeRange = new DateTimeRange(start.getTime(), start.getTime());
			List<FlightInfo> allFlightInfoList = crewFlightExperienceService.listFlightInfoForCrew(dateTimeRange, null,
					null);
			// 2.循环每个人的航班
			if (personList != null && personList.size() > 0) {
				for (ScheduleAuthority person : personList) {
					List<FlightInfo> flightInfoList = flightInfoService.getFlightInfoByWorkcode(allFlightInfoList,
							person.getWorkcode());
					// 3.获取执勤期
					if (flightInfoList != null & flightInfoList.size() > 0) {
						CrewDutyHours crewDutyHours = new CrewDutyHours();
						crewDutyHours.setFlightDate(sdf.format(start.getTime()));
						crewDutyHours.setWorkcode(person.getWorkcode());
						crewDutyHours.setFullName(person.getFullName());
						crewDutyHours.setYear(year);
						int dutyHours = 0;
						dutyHours = DutyDurationUtil.getDutyDurationByFlightInfo(person.getDutyCode(), flightInfoList);
						crewDutyHours.setDutyHours(dutyHours); // 执勤期 是小时或分钟???????????????
						allDutyHours.add(crewDutyHours);
					}
				}
			}
			start.add(Calendar.DATE, 1);
		} while (start.compareTo(end) <= 0);

		// 4.将定时任务获取的信息保存到数据库中
		// 先删除后新增
		if (allDutyHours.size() > 0) {
			crewDutyHoursMapper.batchDelete(start.get(Calendar.YEAR));
			crewDutyHoursMapper.batchInsert(allDutyHours);
		}
		return "CrewDutyHours.job,已完成!";
	}

	@Override
	public Page getPage(String fullName, String dutyCode, Integer year, Integer offset, Integer limit) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

		ScheduleAuthority param = new ScheduleAuthority();
		param.setDutyCode(dutyCode);
		param.setFullName(fullName);
		// 获取人员
		Page page = new Page();
		page.setPageNumber((offset / limit));
		page.setPageSize(limit);
		Page personPage = scheduleAuthorityService.listData(param, page);
		List<ScheduleAuthority> personList = personPage.getRows();
		List<CrewDutyHours> result = new ArrayList<>(); // 每人每月的执勤期----最终返回前端的

		CrewDutyHours crewDutyHours = new CrewDutyHours();
		crewDutyHours.setYear(year);
		if (personList.size() > 0) {
			crewDutyHours.setPersonList(personList);
		}
		List<CrewDutyHours> allDutyHours = this.getDayDutyHours(crewDutyHours); // 所有人员每天的执勤时间

		// 4.遍历人员查询每月的
		if (personList != null && personList.size() > 0 && allDutyHours != null && allDutyHours.size() > 0) {
			for (ScheduleAuthority person : personList) {
				// 2.每个人的航班
				// 放入valueForDates用于通过月份分组
				List<ValueForDate> valueForDateList = new ArrayList<>();
				for (CrewDutyHours dutyHours : allDutyHours) {
					if (dutyHours.getWorkcode().equals(person.getWorkcode())) {
						ValueForDate temp = null;
						try {
							temp = new ValueForDate(sdf.parse(dutyHours.getFlightDate()), dutyHours.getDutyHours());
						} catch (ParseException e) {
							e.printStackTrace();
						}
						valueForDateList.add(temp);
					}
				}
				List<ValueForDate> resultOfPerson = ValueForDateUtil.groupByMonth(valueForDateList); // 每人月度执勤期
				CrewDutyHours crewDutyHour = new CrewDutyHours();
				crewDutyHour.setWorkcode(person.getWorkcode());
				crewDutyHour.setFullName(person.getFullName());
				crewDutyHour.setScheduling(person.getZTechName()); // 排班授权
				crewDutyHour.setDeptName(person.getDeptName()); // 部门
				crewDutyHour.setYear(year);
				crewDutyHour.setDutyOfMonth(resultOfPerson);
				result.add(crewDutyHour);
			}
		}

		PageInfo<CrewDutyHours> pageInfo = new PageInfo<CrewDutyHours>(result);
		personPage.setRows(pageInfo.getList());
		return personPage;
	}

	@Override
	public List<CrewDutyHours> getDayDutyHours(CrewDutyHours crewDutyHours) {
		return crewDutyHoursMapper.getDayDutyHours(crewDutyHours);
	}

	@Override
	public String listItems(String workcode, String month) {
		Calendar start = Calendar.getInstance();
		start.setTimeInMillis(0);
		start.set(Integer.parseInt(month.substring(0, 4)), Integer.parseInt(month.substring(4, 6)) - 1, 1, 0, 0, 0);
		start.getTime(); // 月初

		Calendar end = Calendar.getInstance();
		end.set(Integer.parseInt(month.substring(0, 4)), Integer.parseInt(month.substring(4, 6)), 1, 0, 0, 0);
		end.set(Calendar.DAY_OF_MONTH, 0);
		end.getTime(); // 月末

		CrewDutyHours crewDutyHours = new CrewDutyHours();
		crewDutyHours.setWorkcode(workcode);
		crewDutyHours.setDateTimeRange(new DateTimeRange(start.getTime(), end.getTime()));
		List<CrewDutyHours> allDutyHours = this.getDayDutyHours(crewDutyHours); // 当前人员每天的执勤时间
		return JsonUtils.toJson(allDutyHours);
	}

}
